package com.ly.lypopupwindowdemo.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;
import com.zhy.m.permission.MPermissions;
import com.zhy.m.permission.PermissionDenied;
import com.zhy.m.permission.PermissionGrant;

public class PermissionStudy extends AppCompatActivity {

    private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 6;
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 5;

    private static final int MY_ACCESS_FINE_LOCATION = 100;
    private static final int MY_ACCESS_COARSE_LOCATION = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_study);


        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MPermissions.requestPermissions(PermissionStudy.this, MY_ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
                MPermissions.requestPermissions(PermissionStudy.this, MY_ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION);

            }
        });

        //////////////

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ContextCompat.checkSelfPermission(PermissionStudy.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                    //需要申请权限
                    if (ActivityCompat.shouldShowRequestPermissionRationale(PermissionStudy.this, Manifest.permission.CALL_PHONE)) {

                        Toast.makeText(PermissionStudy.this, "之前拒绝了此权限，请到设置中开启", Toast.LENGTH_SHORT).show();
                    } else {
                        //请求申请权限
                        ActivityCompat.requestPermissions(PermissionStudy.this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
                    }
                } else {
                    //不需要申请权限
                    Toast.makeText(PermissionStudy.this, "已经申请过此权限了", Toast.LENGTH_SHORT).show();
                    callPhone();
                }
            }
        });

        ///////////////////////////
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /**
                 * 第一步在清单文件中添加权限
                 *  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
                 */

                /**
                 * 第二步检查权限
                 * PackageManager.PERMISSION_DENIED---->需要进行申请授权
                 * PackageManage.PERMISSION_GRANTED----->不需要授权
                 */
                if (ContextCompat.checkSelfPermission(PermissionStudy.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                    //需要进行授权
                    /**
                     * 第三步：
                     * 申请授权
                     * 第一个参数是Context
                     * 第二个参数需要申请的字符串数组
                     * 第三个参数requestCode，主要用于回掉时候检测
                     *可以从方法名requestPermissions以及第二个参数看出，是支持一次性申请多个权限的，系统会通过对话框逐一询问用户是否授权。
                     */
                    //ActivityCompat.requestPermissions(PermissionStudy.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

                    if (ActivityCompat.shouldShowRequestPermissionRationale(PermissionStudy.this,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                        /**
                         * 这个API主要用于给用户一个申请权限的解释，该
                         * 方法只有在用户在上一次已经拒绝过你的这个权限申请。也就是说，
                         * 用户已经拒绝一次了，你又弹个授权框，你需要给用户一个解释，为什么要授权，则使用该方法。
                         */
                        //当再次要申请这个权限的时候，如果之前拒绝过了，就会执行这里，提示您已经拒绝过申请这个权限。
                        Toast.makeText(PermissionStudy.this, "您已拒绝了这个权限，请到设置中去开启", Toast.LENGTH_SHORT).show();
                    } else {
                        ActivityCompat.requestPermissions(PermissionStudy.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
                    }
                } else {
                    //不需要
                    //当再次要申请这个权限的时候，就会执行这里，提示您已经申请过了
                    Toast.makeText(PermissionStudy.this, "哈哈您已经申请了write_external_storate权限", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 第四步：
     * 处理权限申请回调
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        switch (requestCode) {
            /**
             * 首先验证requestCode定位到你的申请；
             * 然后验证grantResults对应于申请的结果，这里的数组对应于申请时的第二个权限字符串数组，
             * 如果你同时申请了两个权限，那么grantResult的length就是2，分别记录你两个权限的申请结果。
             *
             */
            case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    //弹出权限申请提示框，如果点击同意就会马上执行这里。
                    Toast.makeText(this, "权限申请成功", Toast.LENGTH_SHORT).show();
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    //弹出权限申请提示框，如果点击拒绝就会马上执行这里。
                    Toast.makeText(this, "您拒绝了权限", Toast.LENGTH_SHORT).show();
                }
                return;
            }

            case MY_PERMISSIONS_REQUEST_CALL_PHONE:

                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(this, "申请拨打电话权限成功", Toast.LENGTH_SHORT).show();
                    callPhone();
                } else {
                    Toast.makeText(this, "您拒绝了拨打电话这个权限", Toast.LENGTH_SHORT).show();
                }
                return;

            case MY_ACCESS_FINE_LOCATION:
                MPermissions.onRequestPermissionsResult(PermissionStudy.this, requestCode, permissions, grantResults);
                return;
            case MY_ACCESS_COARSE_LOCATION:
                MPermissions.onRequestPermissionsResult(PermissionStudy.this, requestCode, permissions, grantResults);
                return;
        }
    }

    private void callPhone() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "17701033723");
        intent.setData(data);
        startActivity(intent);
    }

    @PermissionGrant(MY_ACCESS_FINE_LOCATION)
    public void requestFindLocationSuccess() {
        Toast.makeText(this, "Location权限请求成功", Toast.LENGTH_SHORT).show();
    }

    @PermissionDenied(MY_ACCESS_FINE_LOCATION)
    public void requestFindLocationFailed() {
        Toast.makeText(this, "Loacation权限拒绝", Toast.LENGTH_SHORT).show();
    }

    @PermissionGrant(MY_ACCESS_COARSE_LOCATION)
    public void requestCoarseLocationSuccess() {
        Toast.makeText(this, "Location权限请求成功22", Toast.LENGTH_SHORT).show();
    }

    @PermissionDenied(MY_ACCESS_COARSE_LOCATION)
    public void requestCoarseLocationFailed() {
        Toast.makeText(this, "Loacation权限拒绝22", Toast.LENGTH_SHORT).show();
    }
}
